// ignore_for_file: library_private_types_in_public_api


import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:video_player/video_player.dart';


class VideoView extends StatefulWidget {
  // 数据
  final String url;

  const VideoView({
    required this.url,
    Key? key,
  }) : super(key: key);

  @override
  _VideoViewState createState() => _VideoViewState();
}

class _VideoViewState extends State<VideoView> {
  late VideoPlayerController _controller;

  bool isLoading = true;

  @override
  void initState() {
    super.initState();
    _controller = VideoPlayerController.networkUrl(Uri.parse(widget.url))
      ..initialize().then((_) async {
        isLoading = false;
        await _controller.play();
        setState(() {});
      });
  }

  @override
  void dispose() async {
    super.dispose();
    await _controller.pause();
    await _controller.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Stack(
      children: [
        Center(
          child: _controller.value.isInitialized
              ? AspectRatio(
                  aspectRatio: _controller.value.aspectRatio,
                  child: VideoPlayer(_controller),
                )
              : Container(),
        ),
        Center(
          child: InkWell(
            onTap: () async {
              _controller.value.isPlaying
                  ? await _controller.pause()
                  : await _controller.play();
              setState(() {});
            },
            child: _controller.value.isPlaying
                ? SizedBox(
                    width: 60.w,
                    height: 60.w,
                  )
                : Container(
                    padding: EdgeInsets.all(10.w),
                    decoration: BoxDecoration(
                      color: Colors.white30,
                      // 圆角设置
                      borderRadius: BorderRadius.circular(999),
                    ),
                    child: Icon(
                      _controller.value.isPlaying
                          ? Icons.pause
                          : Icons.play_arrow,
                      size: 40.w,
                    ),
                  ),
          ),
        ),
        if (isLoading)
          const Center(
            child: CircularProgressIndicator(),
          ),
      ],
    );
  }
}
